﻿#region
/**
 * $Date: 0001/1/1
*/
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using BuXiaChu.OrderDishes.DAL.DataAccess;
using BuXiaChu.OrderDishes.Model;
#endregion

namespace BuXiaChu.OrderDishes.DAL
{
    public class DishesCatDAL
    {
        #region private const
        private const string PARAM_CAT_I = "@CatID";
        private const string PARAM_RESTAURANT_I = "@RestaurantID";
        private const string PARAM_NAME = "@Name";
        private const string PARAM_CREATE_DATE = "@CreateDate";
        private const string PARAM_UPDATE_DATE = "@UpdateDate";

        private static readonly string ConnectionString = SQLHelper.CONNECTION_STRING;
        #endregion

        private static void FillModelFrom(SqlDataReader reader, DishesCatModel obj)
        {
            #region
            if (reader != null && !reader.IsClosed)
            {
                obj.CatID = reader.IsDBNull(reader.GetOrdinal("CatID")) ? 0 : reader.GetInt32(reader.GetOrdinal("CatID"));
                obj.RestaurantID = reader.IsDBNull(reader.GetOrdinal("RestaurantID")) ? 0 : reader.GetInt32(reader.GetOrdinal("RestaurantID"));
                obj.Name = reader.IsDBNull(reader.GetOrdinal("Name")) ? String.Empty : reader.GetString(reader.GetOrdinal("Name"));
                obj.CreateDate = reader.IsDBNull(reader.GetOrdinal("CreateDate")) ? DateTime.MinValue : reader.GetDateTime(reader.GetOrdinal("CreateDate"));
                obj.UpdateDate = reader.IsDBNull(reader.GetOrdinal("UpdateDate")) ? DateTime.MinValue : reader.GetDateTime(reader.GetOrdinal("UpdateDate"));
            }
            #endregion
        }

        private static SqlParameter[] GetDishesCatParams(DishesCatModel obj)
        {
            #region
            SqlParameter[] dbParams ={					
					 SQLHelper.MakeParam(PARAM_CAT_I, SqlDbType.Int,0,obj.CatID),
					 SQLHelper.MakeParam(PARAM_RESTAURANT_I, SqlDbType.Int,0,obj.RestaurantID),
					 SQLHelper.MakeParam(PARAM_NAME, SqlDbType.NVarChar, 50,obj.Name)
			};
            
            return dbParams;
            #endregion
        }

        public static bool Save(DishesCatModel obj)
        {
            #region
            try
            {
                SqlParameter[] dbParams = GetDishesCatParams(obj);

                if (obj.CatID == 0)
                {
                    obj.CatID = Convert.ToInt32(SQLHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "DishesCat_InsertUpdate", dbParams));
                    return obj.CatID > 0 ? true : false;
                }
                else
                {
                    var affectNum = SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "DishesCat_InsertUpdate", dbParams);
                    return affectNum > 0 ? true : false;
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("DishesCat-->InsertOrUpdate-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }

            return false;
            #endregion
        }

        public static DishesCatModel GetDishesCatById(int catID)
        {
            #region
            SqlDataReader reader = null;
            DishesCatModel obj = new DishesCatModel();

            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_CAT_I, SqlDbType.Int, 0, catID)
                    };

                reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "DishesCat_GetByDishesCatId", dbParams);

                if (reader.Read())
                {
                    FillModelFrom(reader, obj);
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("DishesCat-->GetSingleObject-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }
            finally
            {
                if (reader != null && !reader.IsClosed)
                    reader.Close();
            }

            return obj;
            #endregion
        }

        public static List<DishesCatModel> GetDishesCatByRestaurantId(int restaurantId)
        {
            #region
            SqlDataReader reader = null;
            List<DishesCatModel> result = new List<DishesCatModel>();

            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_RESTAURANT_I, SqlDbType.Int, 0, restaurantId)
                    };

                reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "DishesCat_GetByRestaurantId", dbParams);

                while (reader.Read())
                {
                    DishesCatModel entity = new DishesCatModel();
                    FillModelFrom(reader, entity);
                    result.Add(entity);
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("DishesCat-->GetDishesCatByRestaurantId-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }
            finally
            {
                if (reader != null && !reader.IsClosed)
                    reader.Close();
            }

            return result;
            #endregion
        }

        public static bool Delete(int catID)
        {
            #region
            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_CAT_I, SqlDbType.Int, 0, catID)
                    };

                return SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "DishesCat_Delete", dbParams) > 0 ? true : false;
            }
            catch (Exception e)
            {
                Exception ex = new Exception("DishesCat-->Delete-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }
            return false;
            #endregion
        }

        public static List<DishesCatModel> Search(string sqlWhere, int pageIndex, int pageSize, out int pageCount)
        {
            #region
            List<DishesCatModel> result = new List<DishesCatModel>();
            pageCount = 0;
            try
            {
                if (string.IsNullOrEmpty(sqlWhere))
                {
                    sqlWhere = " 1=1 ";
                }

                SqlParameter[] dbParams =
                {
                    SQLHelper.MakeParam("@sqlWhere", SqlDbType.NVarChar,0,sqlWhere),
                    SQLHelper.MakeParam("@pageIndex",SqlDbType.Int,0,pageIndex),
                    SQLHelper.MakeParam("@pageSize",SqlDbType.Int,0,pageSize),
                    SQLHelper.MakeOutParam("@pageCount", SqlDbType.Int,0,0)                    
                };
                using (SqlDataReader reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "DishesCat_Search", dbParams))
                {
                    while (reader.Read())
                    {
                        DishesCatModel model = new DishesCatModel();
                        FillModelFrom(reader, model);
                        result.Add(model);
                    }
                }
                pageCount = int.Parse(dbParams[3].Value.ToString());
            }
            catch (Exception e)
            {
                Exception ex = new Exception("DishesCat-->Search-->" + e.ToString());
                ExceptionMessageDAL.Record(ex);
            }
            return result;
            #endregion
        }
    }
}
